package com.zx.article.writer.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zx.article.util.DBUtils;
import com.zx.article.writer.dao.IWriterDao;
import com.zx.article.writer.model.WriterModel;

public class WriterDaoImpl implements IWriterDao {

	private DBUtils dbUtils = new DBUtils();

	@Override
	public List<WriterModel> queryList(WriterModel writerModel) {
		StringBuffer sqlBuffer = new StringBuffer(
				"SELECT w_account,w_password,w_real_name,w_pen_name,w_image,w_phone,w_address,w_email,w_message,( SELECT COUNT(1) FROM article WHERE article.a_writer_account = writer.w_account ) AS count FROM writer ");
		List<Object> appendWhere = appendWhere(sqlBuffer, writerModel);
		return dbUtils.queryList(sqlBuffer.toString(), appendWhere.toArray(), WriterModel.class, getMap());
	}

	@Override
	public WriterModel querModel(String account) {
		String sqlString = "SELECT w_account,w_password,w_real_name,w_pen_name,w_image,w_phone,w_address,w_email,w_message,( SELECT COUNT(1) FROM article WHERE article.a_writer_account = writer.w_account ) AS count FROM writer WHERE w_account = ? ";
		Object[] valObjects = { account };
		return dbUtils.queryOne(sqlString, valObjects, WriterModel.class, getMap());
	}

	@Override
	public String deleteModel(String account) {
		String sqlString = "DELETE FROM writer WHERE w_account = ? ";
		Object[] valObjects = { account };
		return dbUtils.update(sqlString, valObjects);
	}

	@Override
	public String insertModel(WriterModel writerModel) {
		String sqlString = "INSERT INTO writer(w_account,w_password,w_real_name,w_pen_name,w_image,w_phone,w_address,w_email,w_message) VALUES(?,?,?,?,?,?,?,?,?)";
		Object[] valObjects = { writerModel.getAccount(), writerModel.getPassword(), writerModel.getRealName(),
				writerModel.getPenName(), writerModel.getImage(), writerModel.getPhone(), writerModel.getAddress(),
				writerModel.getEmail(), writerModel.getMessage() };
		return dbUtils.update(sqlString, valObjects);
	}

	@Override
	public String updataModel(WriterModel writerModel) {
		StringBuffer sqlBuffer = new StringBuffer("UPDATE writer ");
		List<Object> appendSet = appendSet(sqlBuffer, writerModel);
		return dbUtils.update(sqlBuffer.toString(), appendSet.toArray());
	}

	@Override
	public int queryCount(WriterModel writerModel) {
		StringBuffer sqlString = new StringBuffer("SELECT COUNT(1) FROM writer ");
		List<Object> appendWhere = appendWhere(sqlString, writerModel);
		return dbUtils.queryCount(sqlString.toString(), appendWhere.toArray());
	}

	private List<Object> appendWhere(StringBuffer sqlBuffer, WriterModel writerModel) {
		List<Object> list = new ArrayList<>();
		sqlBuffer.append("where 1=1 ");
		String account = writerModel.getAccount();
		if (account != null && !account.isEmpty()) {
			sqlBuffer.append("and w_account like ? ");
			list.add(account);
		}
		String realName = writerModel.getRealName();
		if (realName != null && !realName.isEmpty()) {
			sqlBuffer.append("and w_real_name like ? ");
			list.add(realName);
		}
		String penName = writerModel.getPenName();
		if (penName != null && !penName.isEmpty()) {
			sqlBuffer.append("and w_pen_name like ? ");
			list.add(penName);
		}
		if (writerModel.isPageOn()) {
			sqlBuffer.append("limit ?,? ");
			list.add(writerModel.getRowStart());
			list.add(writerModel.getRowCount());
		}
		return list;
	}

	private List<Object> appendSet(StringBuffer sqlBuffer, WriterModel writerModel) {
		List<Object> list = new ArrayList<>();
		String account = writerModel.getAccount();
		list.add(account);
		sqlBuffer.append("set w_account = ?");
		String realName = writerModel.getRealName();
		if (realName != null && !realName.isEmpty()) {
			sqlBuffer.append(",w_real_name = ?");
			list.add(realName);
		}
		String password = writerModel.getPassword();
		if (password != null && !password.isEmpty()) {
			sqlBuffer.append(",w_password = ?");
			list.add(password);
		}
		String penName = writerModel.getPenName();
		if (penName != null && !penName.isEmpty()) {
			sqlBuffer.append(",w_pen_name = ?");
			list.add(penName);
		}
		String phone = writerModel.getPhone();
		if (phone != null && !phone.isEmpty()) {
			sqlBuffer.append(",w_phone = ?");
			list.add(phone);
		}
		String email = writerModel.getEmail();
		if (email != null && !email.isEmpty()) {
			sqlBuffer.append(",w_email = ?");
			list.add(email);
		}
		String message = writerModel.getMessage();
		if (message != null && !message.isEmpty()) {
			sqlBuffer.append(",w_message = ?");
			list.add(message);
		}
		String image = writerModel.getImage();
		if (image != null && !image.isEmpty()) {
			sqlBuffer.append(",w_image = ?");
			list.add(image);
		}
		sqlBuffer.append(" where w_account = ?");
		list.add(account);
		return list;
	}

	private Map<String, String> getMap() {
		Map<String, String> map = new HashMap<>();
		map.put("account", "w_account");
		map.put("password", "w_password");
		map.put("realName", "w_real_name");
		map.put("penName", "w_pen_name");
		map.put("image", "w_image");
		map.put("phone", "w_phone");
		map.put("address", "w_address");
		map.put("email", "w_email");
		map.put("message", "w_message");
		map.put("count", "count");
		return map;
	}

}
